package com.example.springboot.service.impl;

import com.example.springboot.utils.SqlString;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.example.springboot.dao.ResourceMapper;
import com.example.springboot.domain.Resource;
import com.example.springboot.domain.ResourseVo;
import com.example.springboot.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @javax.annotation.Resource
    private ResourceMapper resourceMapper;


//    @Override
//    public PageInfo<Resource> findAllResourceByPage(ResourseVo resourseVo) {
//
//        //分页查询
//        PageHelper.startPage(resourseVo.getCurrentPage(),resourseVo.getPageSize());
//
//        List<Resource> allResourceByPage = resourceMapper.findAllResourceByPage(resourseVo);
//
//        PageInfo<Resource> pageInfo = new PageInfo<>(allResourceByPage);
//
//        return pageInfo;
//    }

    @Override
    public PageInfo<Resource> findAllResourceByPage(ResourseVo resourseVo) {
        resourseVo.setName(SqlString.replaceLike(resourseVo.getName()));
        List<Resource> Info = resourceMapper.findAllResourceByPage(resourseVo);
        int totalPages =resourceMapper.getTotal(resourseVo);
        PageInfo<Resource> pageInfo = new PageInfo<>();
        pageInfo.setList(Info);
        int pages=totalPages/resourseVo.getPageSize();
        if(resourseVo.getCurrentPage()<=pages){
            pageInfo.setHasNextPage(true);
        }else {
            pageInfo.setHasNextPage(false);
        }
        pageInfo.setPages(pages);
        pageInfo.setTotal(totalPages);
        return pageInfo;
    }

    @Override
    public void addResource(Resource resource) {
        Date date = new Date();
        resource.setCreatedTime(date);
        resource.setUpdatedTime(date);
        resource.setCreatedBy("system");
        resource.setUpdatedBy("system");
        resourceMapper.addResource(resource);

    }

    @Override
    public void updateResource(Resource resource) {
        Date date = new Date();
        resource.setUpdatedTime(date);
        resource.setUpdatedBy("system");
        resourceMapper.updateResource(resource);
    }

    @Override
    public void deleteResource(Integer id) {
        resourceMapper.deleteRoleResourceRelation(id);
        resourceMapper.deleteResource(id);
    }
}
